package com.Shoping.controller;

import com.Shoping.entity.*;
import com.Shoping.entity.Integrate.CommodityX;
import com.Shoping.entity.Integrate.CommodityY;
import com.Shoping.entity.Integrate.EditItemX;
import com.Shoping.service.CategoryService;
import com.Shoping.service.ColorService;
import com.Shoping.service.CommodityService;
import com.Shoping.service.impl.CategoryServiceImpl;
import com.Shoping.service.impl.ColorServiceImpl;
import com.Shoping.service.impl.CommodityServiceImpl;
import com.Shoping.util.UploadUtils;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@WebServlet(value = "/commodity")
@MultipartConfig(maxRequestSize = 1024*1024*10,maxFileSize = 1024*1024*100)
public class CommodityController extends BaseServlet {
    public String European(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        CommodityService commodityService = new CommodityServiceImpl();
        List<Commodity> European = commodityService.selectByCgId(1);
        List<Commodity> Modern = commodityService.selectByCgId(2);
        List<Commodity> xiangxun = commodityService.selectByCgId(3);
        CommodityX commodityX = new CommodityX(European,Modern,xiangxun);
        return JSON.toJSONString(commodityX);
    }

    public String selectByCmId(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        Integer cmId = Integer.parseInt(request.getParameter("cmId"));
        CommodityService commodityService = new CommodityServiceImpl();
        Commodity commodity = commodityService.selectByCmId(cmId);
        List<Color> colors = commodityService.selectColorByCmId(cmId);
        HttpSession session = request.getSession();
        session.setAttribute("colors",colors);
        CommodityY commodityY = new CommodityY(commodity,colors);
        return JSON.toJSONString(commodityY);
    }

    public String show(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        String currentPath1 = request.getParameter("currentPath");//当前页码
        int currentPath = 1;
        if(currentPath1!=null){
            currentPath = Integer.parseInt(currentPath1);
        }
        int pageSize = 11;//一页数据行数

        CommodityService commodityService = new CommodityServiceImpl();
        List<Commodity> list = commodityService.selectByPath(pageSize,currentPath);
        long totalCount = commodityService.count();
        PageBean<Commodity> pageBean = new PageBean<>(list,currentPath,pageSize,totalCount);
        request.getSession().setAttribute("pageBean",pageBean);

        return JSON.toJSONString(pageBean);
    }

    public void modifyCommodityByCmId(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        Integer cmId = Integer.parseInt(request.getParameter("cmId"));
        CommodityService commodityService = new CommodityServiceImpl();
        ColorService colorService = new ColorServiceImpl();
        colorService.modifyColorByCmId(cmId);
        commodityService.modifyCommodityByCmId(cmId);
    }

    public String EditItem(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        Integer cmId = Integer.parseInt(request.getParameter("cmId"));

        CommodityService commodityService = new CommodityServiceImpl();
        Commodity commodity = commodityService.selectByCmId(cmId);

        ColorService colorService = new ColorServiceImpl();
        List<Color> list = colorService.selectByCmId(commodity.getCmId());

        CategoryService categoryService = new CategoryServiceImpl();
        Category category = categoryService.selectByCgId(commodity.getCgId());

        EditItemX editItemX = new EditItemX(commodity.getCmName(),category.getCgName(),list,commodity.getCmDetails(),commodity.getCmPrice(),commodity.getCmImg(),commodity.getCgId());
        return JSON.toJSONString(editItemX);
    }

    public String updateCommodity(HttpServletRequest request,HttpServletResponse response) throws SQLException {

        //颜色对应的id，名称，数量
        Integer cId0 = Integer.parseInt(request.getParameter("cId0"));
        String cName0 = request.getParameter("cName0");
        Double cNumber0 = Double.parseDouble(request.getParameter("cNumber0"));
        Integer cId1 = Integer.parseInt(request.getParameter("cId1"));
        String cName1 = request.getParameter("cName1");
        Double cNumber1 = Double.parseDouble(request.getParameter("cNumber1"));
        Integer cId2 = Integer.parseInt(request.getParameter("cId2"));
        String cName2 = request.getParameter("cName2");
        Double cNumber2 = Double.parseDouble(request.getParameter("cNumber2"));
        Integer cId3 = Integer.parseInt(request.getParameter("cId3"));
        String cName3 = request.getParameter("cName3");
        Double cNumber3 = Double.parseDouble(request.getParameter("cNumber3"));
        //商品id
        Integer cmId = Integer.parseInt(request.getParameter("cmId"));
        Double cmPrice = Double.parseDouble(request.getParameter("cmPrice"));
        String cmDetails = request.getParameter("cmDetails");
        //商品的图片路径
        String cmImg = request.getParameter("cmImg");
        //商品名称
        String cmName = request.getParameter("cmName");
        //商品类别id
        Integer cgId = Integer.parseInt(request.getParameter("cgId"));

        ColorService colorService = new ColorServiceImpl();
        colorService.updateColor(cId0,cName0,cNumber0);
        colorService.updateColor(cId1,cName1,cNumber1);
        colorService.updateColor(cId2,cName2,cNumber2);
        colorService.updateColor(cId3,cName3,cNumber3);

        Commodity commodity = new Commodity(cmId,cgId,cmName,cmPrice,cmDetails,cmImg);
        CommodityService commodityService = new CommodityServiceImpl();
        commodityService.updateCommodity(commodity);

        return Constants.REDIRECT+"/ProgramJSP/goods-Manage.jsp";
    }

    public String fileCommodityImg(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        String realPath = request.getServletContext().getRealPath("/img/goktech-img/images");
        File file = new File(realPath);
        if(!file.exists()) {
            file.mkdirs();
        }
        Collection<Part> parts = request.getParts();
        if(parts!=null) {
            for (Part part : parts) {
                String fileName = part.getSubmittedFileName();
                if(fileName!=null) {
                    String newFileName = UploadUtils.getFileName(fileName);
                    part.write(realPath+"\\"+newFileName);
                    String str = realPath+"\\"+newFileName;
                    System.out.println(JSON.toJSONString(str.substring(str.indexOf("img"))));
                    return JSON.toJSONString(str.substring(str.indexOf("img")));
                }else {
                    System.out.println(part.getName()+":"+request.getParameter(part.getName()));
                }
            }
        }
        return null;
    }

    public String addCommodity(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        Integer cgId = Integer.parseInt(request.getParameter("cgId"));
        String cmName = request.getParameter("cmName");
        Double cmPrice = Double.parseDouble(request.getParameter("cmPrice"));
        String cmDetails = request.getParameter("cmDetails");
        String cmImg = request.getParameter("cmImg");

        CommodityService commodityService = new CommodityServiceImpl();
        commodityService.addCommodity(cgId,cmName,cmPrice,cmDetails,cmImg);
        Integer cmId = commodityService.selectCmId();

        Integer cNumber0 = Integer.parseInt(request.getParameter("cNumber0"));
        Integer cNumber1 = Integer.parseInt(request.getParameter("cNumber1"));
        Integer cNumber2 = Integer.parseInt(request.getParameter("cNumber2"));
        Integer cNumber3 = Integer.parseInt(request.getParameter("cNumber3"));

        ColorService colorService = new ColorServiceImpl();
        colorService.addColor(cmId,"绿色",cNumber0);
        colorService.addColor(cmId,"橙色",cNumber1);
        colorService.addColor(cmId,"粉色",cNumber2);
        colorService.addColor(cmId,"白色",cNumber3);
        return Constants.REDIRECT+"/ProgramJSP/goods-Manage.jsp";
    }
}
